import java.io.IOException;
import java.util.Scanner;

public class ClientATM extends BankomatMethods {

    private static final String USERLOGGEDOFF = "You were logged out";
    private static final String SELECT_MENU = "\nPlease, select the action: ... \n\nTo view balance enter               1\n\nTo withdraw from account enter      2\n\nTo exit press                       0\n";
    private static final String ENTER_SUM = "Please, enter the sum to withdraw in format NNNN, like 0051\n\n The maximum sum for 1 transaction should not be more than 9999\n";
    private static final String ERROR_OCCURED = "Error occured, please, try again";
    private static final String OUTPUT_INFO = "OUTPUT_INFORMATION_TO_SCREEN";
    private static final String ERROR_TRY_AGAIN = "ERROR, YOU WHERE LOGGED OUT, PLEASE TRY AGAIN ";
    private static final String INSTRUCTION_TO_SHOW = "SHOW";
    private static final String INSTRUCTION_TO_GET = "GETM";
    private static final String INPUT_PIN = "input PIN 1234 to use the device\n";

    public static void main(String[] args) {
        while (true) {
            sayHello();
            choseOption();
        }
    }

    private static void choseOption() {
        Scanner sc = new Scanner(System.in);
        int c = sc.nextInt();
        switch (c) {
            case 0:
                System.out.println(USERLOGGEDOFF);
                System.exit(0);
                break;
            case 1:
                clearUserScreen();
                System.out.println(SELECT_MENU);
                int action = sc.nextInt();
                choseInternalOption(action);
                break;
            case 2:
                System.out.println(OUTPUT_INFO);
                ClientATM obInfo = new ClientATM();
                obInfo.getInfo();
                break;
            default:
                System.out.println(ERROR_TRY_AGAIN);
                System.exit(0);
        }
    }

    private static void choseInternalOption(int action) {
        switch (action) {
            case 0:
                System.exit(0);
                break;
            case 1:
                requestShowBalance();
                break;
            case 2:
                requestForTransaction();
                break;
            default:
                System.out.println(ERROR_OCCURED);
                System.exit(0);
        }
    }

    private static void requestForTransaction() {
        clearUserScreen();
        Integer inputPin2 = getPin();
        ClientATM clientOn2 = new ClientATM();
        System.out.println(ENTER_SUM);
        Scanner scanner = new Scanner(System.in);
        String putcash = scanner.nextLine();
        try {
            clientOn2.connectToServerAndProvideTransaction(inputPin2, putcash, INSTRUCTION_TO_GET);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void requestShowBalance() {
        clearUserScreen();
        Integer inputPin = getPin();
        ClientATM clientOn = new ClientATM();
        try {
            clientOn.connectToServerAndProvideTransaction(inputPin, null, INSTRUCTION_TO_SHOW);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return;
    }

    private static Integer getPin() {
        System.out.println(INPUT_PIN);
        Scanner auth = new Scanner(System.in);
        return auth.nextInt();
    }
}